﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Runtime.Serialization.Json;

namespace MvcEngine.Mvc.Filters
{
    public class JsonActionFilter : ActionFilterAttribute
    {
        public string ParamName { get; set; }

        public Type Type { get; set; }

        public JsonActionFilter(Type type, string paramName)
        {
            this.Type = type;
            this.ParamName = paramName;
        }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            object obj = new DataContractJsonSerializer(Type).ReadObject(filterContext.HttpContext.Request.InputStream);
            filterContext.ActionParameters[ParamName] = obj;
        }
    }
}
